package br.facet.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.facet.pojo.Fornecedor;
import br.facet.utils.HibernateUtils;

public class FornecedorDAO {

	public void salvar(Fornecedor fornecedor){

		Session sessao = null;
		Transaction tx = null;

		try{
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();
			sessao.save(fornecedor);
			tx.commit();
		}
		catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}
		finally{
			if(sessao != null)
				try{
					sessao.close();
				}catch(Exception e2){
					e2.printStackTrace();
				}
		}
	}


	@SuppressWarnings("unchecked")
	public List<Fornecedor> listar(){
		List<Fornecedor> lista = new ArrayList<Fornecedor>();
		Session sessao = null;
		Transaction tx = null;

		try{
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();
			lista = sessao.createCriteria(Fornecedor.class).list();

		}
		catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}
		finally{
			if(sessao != null)
				try{
					sessao.close();
				}catch(Exception e2){
					e2.printStackTrace();
				}
		}
		return lista;
	}

	@SuppressWarnings("unused")
	public List<Fornecedor> listarPorNome(String nome){
		List<Fornecedor> lista = new ArrayList<Fornecedor>();
		Session sessao = null;
		Transaction tx = null;

		try{
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();
			Query sql = sessao.createSQLQuery("Select * from tbl_fornecedor where nome = :nome").setParameter("nome", nome);
			
			
		}
		catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}
		finally{
			if(sessao != null)
				try{
					sessao.close();
				}catch(Exception e2){
					e2.printStackTrace();
				}
		}
		return lista;
	}

	public void excluirFornecedor(Fornecedor fornecedor){
		Session sessao = null;
		Transaction tx = null;

		try{
			sessao = HibernateUtils.getSession();
			tx= sessao.beginTransaction();
			sessao.delete(fornecedor);
			tx.commit();

		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}
		finally{
			if(sessao != null)
				try{
					sessao.close();
				}catch (Exception e2) {
					e2.printStackTrace();
				}
		}
	}

	public String obterFornecedorPorMarca(Integer codMarca){

		Session sessao = null;
		Transaction tx = null;
		String retorno = null;

		try {
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();
			Query sql = sessao.createSQLQuery("Select * from tbl_fornecedor where id_marca = :codMarca").setParameter("codMarca", codMarca);

			if(sql.uniqueResult() == null){
				retorno = "inexistente";
			}
			else{

				retorno ="existe";
			}

			tx.commit();

		} catch (Exception e) {
			tx.rollback();
			retorno = "erro";
		} finally {
			if(sessao != null){
				try {
					sessao.close();					
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}

		}
		return retorno;
	}

}
